 package com.hongyu.smart.village.ui.activity;

 import android.annotation.SuppressLint;
 import android.net.Uri;
 import android.os.Bundle;

 import androidx.media3.common.MediaItem;
 import androidx.media3.datasource.DataSource;
 import androidx.media3.datasource.DefaultDataSource;
 import androidx.media3.datasource.rtmp.RtmpDataSource;
 import androidx.media3.exoplayer.ExoPlayer;
 import androidx.media3.exoplayer.hls.HlsMediaSource;
 import androidx.media3.exoplayer.rtsp.RtspMediaSource;
 import androidx.media3.exoplayer.source.MediaSource;
 import androidx.media3.exoplayer.source.ProgressiveMediaSource;
 import androidx.media3.ui.PlayerView;

 import com.hongyu.smart.village.R;
 import com.hongyu.smart.village.event.Event;
 import com.hongyu.smart.village.ui.base.BaseActivity;

 import butterknife.BindView;

 public class VideoPalyActivity extends BaseActivity {
     @BindView((R.id.pv_content))
     public PlayerView playerView;
     private ExoPlayer exoPlayer;
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

     @Override
     protected void initActivityData() {

     }

     @Override
     protected void initContentView() {
         exoPlayer = new ExoPlayer.Builder(this).build();
         playerView.setPlayer(exoPlayer);
     }
     // 播放视频
     @SuppressLint("UnsafeOptInUsageError")
     private void playVideo(Uri uri) {
         DataSource.Factory factory = new DefaultDataSource.Factory(this);
         // 创建指定地址的媒体对象
         MediaItem videoItem = new MediaItem.Builder().setUri(uri).build();
         // 基于工厂对象和媒体对象创建媒体来源
         MediaSource videoSource;
         if (uri.getPath().endsWith("m3u8")) { // hls链接
             videoSource = new HlsMediaSource.Factory(factory)
                     .createMediaSource(videoItem);
         } else if (uri.getPath().startsWith("rtsp")) { // rtsp链接
             videoSource = new RtspMediaSource.Factory()
                     .createMediaSource(videoItem);
         } else if (uri.getPath().startsWith("rtmp")) { // rtmp链接
             videoSource = new ProgressiveMediaSource.Factory(new RtmpDataSource.Factory())
                     .createMediaSource(videoItem);
         } else { // 其他链接（http开头或https开头的普通视频链接）
             videoSource = new ProgressiveMediaSource.Factory(factory)
                     .createMediaSource(videoItem);
         }
         exoPlayer.setMediaSource(videoSource); // 设置播放器的媒体来源
         exoPlayer.prepare(); // 播放器准备就绪
         exoPlayer.play(); // 播放器开始播放
     }

     @Override
     protected int getContentView() {
         return R.layout.activity_video_paly;
     }

     @Override
     protected void onEventProcess(Event event) {

     }
     @Override
     protected void onDestroy() {
         super.onDestroy();
         //释放资源
         exoPlayer.release();
     }

 }